home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / ABUSESRC.ZIP / AbuseSrc / macabuse / imlib / include / linked.hpp < prev    next >
C/C++ Source or Header  |  1997-05-20  |  4KB  |  103 lines

  1. // linked.hpp  - linked list and linked list node classes
  2. // written June 2, 1992 by Jonathan Clark  (at home)
  3. // these classes provide the basic groundwork for any future linked list
  4. // please derive your own linked_node subclass and define the virtual
  5. // function compare.
  6. // example compare function
  7. //   virtual int compare(void *n1, int field)
  8. //        {return ((classname *) n1)->data > data);}
  9. //  should return (1 if n1 is greater than (self)) else return 0
  10. //  field is the value determined by linked_list::set_sort_field
  11. //  this defaults to 1
  12.  
  13.  
  14. #ifndef linkman         
  15. #define linkman
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19.  
  20. #define loop(controll,first,inside) { (linked_node *)controll=first; \
  21.   if (first) do { inside (linked_node *) controll=controll->next(); } \
  22.   while ((linked_node *) controll!=first); }
  23.  
  24. #define loopt(type,controll,first,inside) { controll=(type *)(first); \
  25.   if (first) do { inside controll=(type *)(controll->next()); } \
  26.   while (controll!=(type *)(first)); }
  27.  
  28.  
  29. #define loop_rev(controll,last,inside) { (linked_node *)controll=last; \
  30.   if (first) do { inside (linked_node *) controll=controll->last(); } \
  31.   while ((linked_node *) controll!=last); }
  32.  
  33. #define loopct(type,controll,first,cond,inside) { controll=(type *)first; \
  34.   if (first && (cond)) do { inside controll=(type *)controll->next(); } \
  35.   while (controll!=(type *)first && (cond)); }
  36.  
  37. #define loop_fort(type,controll,first,x) \
  38.   int x=0; \
  39.   if (first) \
  40.      for (controll=(type *)(first); \
  41.           (!x || (controll)!=(type *)(first));\
  42.           controll=(type *)(controll->next()),x++)
  43.  
  44. #define loop_forct(type,controll,first,cond,x) int x=0; if (first) for \
  45.   (controll=(type *)(first);cond && (!x || controll!=(type *)(first));\
  46.   controll=(type *)(controll->next()),x++)
  47.  
  48. class linked_node
  49. {
  50.   class linked_node *nextp, *lastp;
  51. public:
  52.   virtual int compare(void *n1, int field)     {return(0);}  // default is = (equal)
  53.   class linked_node *next()              {return nextp;}
  54.   class linked_node *last()              {return lastp;}
  55.   void set_next(class linked_node *p)    {nextp=p;}
  56.   void set_last(class linked_node *p)    {lastp=p;}
  57.   virtual ~linked_node() { ; }
  58.   linked_node() { nextp=NULL; lastp=NULL; }
  59. };
  60.  
  61. // this is the basic class for all linked_list
  62. // it's features should beself explanitory
  63. // openly use the functions listed after the keyword PUBLIC
  64. // type conversions may be nessary if you derive a class of nodes of your own
  65. // for example shape is an class derived from linked_node.
  66. // to add a shape to linked lis I have to say
  67. // mylist.add_end( (linked_node *) myshape_pointer);
  68. // unlink removes a node from the list via pointers but does not deallocate
  69. // it from the heap
  70. // the destructor for linked_list will get dispose of all the nodes as
  71. // well, so if you don't want something deleted then you must unlink
  72. // it from the list before the destructor is called
  73.  
  74. class linked_list
  75. {
  76.   class linked_node *fn, *cn;     // first and current nodes
  77.   int nn; char sortby;
  78. public :
  79.   linked_list(linked_node *first=NULL);
  80.   void add_front(class linked_node *p);
  81.   void add_end(class linked_node *p);
  82.   void insert(class linked_node *p);
  83.   void set_sort_field(int x) {sortby=x;}   // this is passed to compare
  84.   class linked_node *current() {return cn;}
  85.   class linked_node *first() {return fn;}
  86.   class linked_node *last() {return fn->last();}
  87.   class linked_node *get_node(int x);
  88.   void set_current(class linked_node *p) {cn=p;}
  89.   void go_first() {cn=fn;}
  90.   void go_end() {cn=fn->last();}
  91.   void go_next() {cn=cn->next();}
  92.   void go_last() {cn=cn->last();}
  93.   int number_nodes()  {return nn;}
  94.   int node_number(linked_node *p);
  95.   int unlink(linked_node *p);
  96.   ~linked_list();
  97. };
  98.  
  99. #endif
  100.  
  101.  
  102.  
  103.